package com.gome.ocean.common.utils;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.gome.ocean.common.enums.SmsUrl;

/**
 * 发送短信接口
 * 
 * @author haqiaolong 2016年8月9日 上午11:48:57
 * @since 5.0.0
 */
public final class SmsUtils {

    private static final Logger LOG = LoggerFactory.getLogger(SmsUtils.class);

    private static final String URL = "http://10.58.24.126:801/QgService.asmx/SendSaleSMS?phone=%s&context=%s&priority=1";

    private static final String CONTEXT = "【国美在线】您的登陆验证码为：%s,请妥善保管,切勿告诉他人。";

    private SmsUtils(){
    }

    public static boolean sendSms(String phone, String code) {
        HttpClientUtil httpClientUtil = new HttpClientUtil();
        String smsUrl = getUrl(phone, code);
        try {
            URL url = new URL(smsUrl);
            HttpGet httpGet = HttpClientUtil.getGetRequest();
            httpGet.setURI(url.toURI());
            String result = httpClientUtil.executeAndGetWithFailedRetry(httpGet, 3, 1000l);
            if (StringUtils.isNotEmpty(result)) {
                LOG.info("短信发送成功！:" + smsUrl);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            LOG.error("短信发送失败！", e);
            return false;
        }
    }

    // 利用新的短信平台
    public static boolean sendSms(String env, String phone, String code, String businessName, String templateId) {

        String smsUrl = SmsUrl.getSmsUrl(env.toLowerCase());

        HttpPost httpPost = new HttpPost(smsUrl);
        HttpClientUtil httpClientUtil = new HttpClientUtil();
        try {

            // 设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("businessName", businessName));
            list.add(new BasicNameValuePair("phone", phone));
            list.add(new BasicNameValuePair("templateId", templateId));
            list.add(new BasicNameValuePair("parameter", getParamValue(code)));
            // url格式编码
            LOG.info("POST 请求...." + httpPost.getURI() + "验证码为：" + code);
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
            LOG.info("短信参数:" + entity);
            httpPost.setEntity(entity);
            String result = httpClientUtil.executeAndGetWithFailedRetry(httpPost, 3, 1000l);
            if (StringUtils.isNotEmpty(result)) {
                LOG.info("短信接口返回:" + result);
                LOG.info("短信发送成功！:" + smsUrl);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            LOG.error("短信发送失败！", e);
            return false;
        }
    }

    private static String getUrl(String phone, String code) {
        return String.format(URL, phone, String.format(CONTEXT, code));
    }

    private static String getParamValue(String code) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("identifyingCode", code);
        return jsonObject.toJSONString();
    }
}
